<?php

@ini_set("track_errors", "1");
@set_magic_quotes_runtime(0);
//error_reporting(0);
$devedit_internal_possible_products = array
(
	// iem
	array(
		'exists'	=> '/../com/init.php',
		'record'	=> '/../com/storage/session_files',
		'product'	=> 'iem',
	),
	// sendstudio
	array(
		'exists'	=> '/../functions/sendstudio_functions.php',
		'include'	=> '/../functions/init.php',
		'record'	=> '/../temp',
		'product'	=> 'ssnx',
	),
	// webedit nx 1.3
	array(
		'exists'	=> '/../classes/class.page.php',
		'include'	=> '/../../session.php',
		'product'	=> 'webedit',
	),
	// ISC
	array(
		'exists'	=> '/../includes/classes/class.froogle.php',
		'include'	=> '/../init.php',
		'product'	=> 'isc',
	),
	// IWP 4.5
	array(
		'exists'	=> '/../includes/classes/class.blogs.php',
		'include'	=> '/../index.php',
		'product'	=> 'iwp',
	),
	// IKM
	array(
		'exists'	=> '/../includes/classes/class.question.php',
		'include'	=> '/../../init.php',
		'product'	=> 'ikm',
	),
);

$devedit_internal_found = false;
foreach($devedit_internal_possible_products as $devedit_internal_is_product)
{
	if (!file_exists(dirname(__FILE__) . $devedit_internal_is_product['exists'])) {
		continue;
	}

	if ((isset($devedit_internal_is_product['include'])) && (file_exists(dirname(__FILE__) . $devedit_internal_is_product['include']))) {
		$devedit_internal_found = true;
		if ($devedit_internal_is_product['product'] == 'iwp'){
			$skipLoad = true;
		}
		require_once(dirname(__FILE__) . $devedit_internal_is_product['include']);
		$found = true;
	}
	if ((isset($devedit_internal_is_product['record'])) && (is_writeable(dirname(__FILE__) . $devedit_internal_is_product['record']))) {
		$devedit_internal_found = true;
		$found = true;
	}

	if (isset($devedit_internal_is_product['product'])) {
		switch ($devedit_internal_is_product['product']) {
			case 'isc':
				$authClass = GetClass('ISC_ADMIN_AUTH');
				if (!$authClass->IsLoggedIn()) {
					exit;
				}
				$_SERVER['DOCUMENT_ROOT'] = ISC_BASE_PATH;
				break;
			case 'iwp':
				$authClass = &GetAdminClass('auth');
				if (!$authClass->IsLoggedIn()) {
					die('Authentication check failed. Please make sure you are logged in. You can <a href="../" target="_blank">login to your control panel here</a>.');
				}
				$_SERVER['DOCUMENT_ROOT'] = AL_BASE_PATH;
				break;
			case 'iem':
				if (!class_exists('IEM', false)) {
					if (!defined('IEM_NO_CONTROLLER')) {
						define('IEM_NO_CONTROLLER', true);
					}
					require_once(dirname(__FILE__) . '/../com/init.php');
					$user = IEM::getCurrentUser();
					if (empty($user)) {
						exit;
					}
				}
				break;
			case 'ssnx':
				if (!defined('SENDSTUDIO_IS_SETUP')) {
					@ini_set('session.save_handler', 'files');
					@ini_set('session.save_path', dirname(__FILE__) . $devedit_internal_is_product['record']);
					@ini_set('session.gc_probability', 1);
					@ini_set('session.gc_divisor', 100);
					@ini_set('session.gc_maxlifetime', 3600);
					@session_start();
				}
				$session = &GetSession();
				if (!$session->LoggedIn()) {
					exit;
				}
				break;
			case 'ikm':
				if (!IsLoggedIn()) {
					exit;
				}
				// Authenticate
				$_SERVER['DOCUMENT_ROOT'] = AKB_ROOT;
				break;
		}
	}

	// If we have a match, break out
	if (isset($found)) {
		break;
	}
}

if ($devedit_internal_found === false) {
	@session_start();
}

/*
For some reason some servers do not have these defines set which can cause
strange errors when uploading
*/
$devedit_internal_missing_constants = array
(
	'UPLOAD_ERR_OK'			=> 0,
	'UPLOAD_ERR_INI_SIZE'	=> 1,
	'UPLOAD_ERR_FORM_SIZE'	=> 2,
	'UPLOAD_ERR_PARTIAL'	=> 3,
	'UPLOAD_ERR_NO_FILE'	=> 4,
	'UPLOAD_ERR_NO_TMP_DIR'	=> 6,
	'UPLOAD_ERR_CANT_WRITE'	=> 7,
	'UPLOAD_ERR_EXTENSION'	=> 8
);
foreach($devedit_internal_missing_constants as $devedit_internal_missing_constant=>$devedit_internal_value) {
	if (!defined($devedit_internal_missing_constant)) {
		define($devedit_internal_missing_constant, $devedit_internal_value);
	}
}

require_once(dirname(__FILE__).'/valid_files.php');
